package org.example.springboot3.service;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.example.springboot3.entity.Employee;
import org.example.springboot3.exception.CustomException;
import org.example.springboot3.mapper.EmployeeMapper;
import org.example.springboot3.utils.TokenUtils;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    //@Service：将 EmployeeService 类标记为服务层组件，Spring 会自动扫描并将其纳入容器管理。
    //@Resource：注入 EmployeeMapper 实例，用于与数据库进行交互。

    public List<Employee> selectALL(Employee employee) {
        return employeeMapper.selectAll(employee);
    }

    public Employee selectById(Integer id) {
        return employeeMapper.selectById(id);
    }

    public Employee selectByUsername (String username) {
        return employeeMapper.selectByUsername(username);
    }

    public PageInfo<Employee> selectPage(Employee employee, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> list = employeeMapper.selectAll(employee);
        return PageInfo.of(list);//PageInfo.of(list)：将查询结果封装成 PageInfo 对象，包含分页信息
    }

    public void add(Employee employee) {
        String username = employee.getUsername();  // 账号
        Employee dbEmployee = employeeMapper.selectByUsername(username);
        if (dbEmployee != null) {  // 注册的账号已存在  无法注册
            throw new CustomException("500", "账号已存在，请更换别的账号");
        }
        if (StrUtil.isBlank(employee.getPassword())) {  // 密码没填
            employee.setPassword("123");  // 默认密码 123
        }
        if (StrUtil.isBlank(employee.getName())) {  // 名字没填
            employee.setName(employee.getUsername());  // 默认名称
        }
        // 一定要设置角色
        employee.setRole("EMP");  // 员工的角色
        employeeMapper.insert(employee);
    }

    public void updata(Employee employee) {
        employeeMapper.updataById(employee);
    }

    public void deleteById(Integer id) {
        employeeMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            this.deleteById(id);
        }//遍历员工 ID 列表，调用 deleteById 方法逐个删除员工信息。
    }

    public Employee login(Employee employee) {
        String username = employee.getUsername();  // 账号
        Employee dbEmployee = employeeMapper.selectByUsername(username);
        if (dbEmployee == null) {  // 没查询到任何用户   说明没有这个账号
            throw new CustomException("500", "账号不存在");
        }
        // 数据库存在这个账号
        String password = employee.getPassword();
        if (!dbEmployee.getPassword().equals(password)) {  // 用户输入的密码跟数据库账号的密码不匹配
            throw new CustomException("500", "账号或密码错误");
        }
        //生成JWT
        String token = TokenUtils.createToken(dbEmployee.getUsername()+"-"+ dbEmployee.getRole(),dbEmployee.getPassword());
        dbEmployee.setToken(token);
        return dbEmployee;
    }

    public void register(Employee employee) {this.add(employee);}

    public void updatePassword(Employee employee) {
        // 1. 校验两次输入的新密码是否一致
        if (!employee.getNewpwd().equals(employee.getCheckpwd())) {
            throw new CustomException("500", "新密码与确认密码不一致");
        }
        // 2. 获取当前登录用户
        Employee currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null) {
            throw new CustomException("500", "当前用户未登录");
        }
        // 3. 验证原密码是否正确（从数据库重新获取最新用户数据）
        Employee dbEmployee = employeeMapper.selectById(currentUser.getId());
        if (!employee.getPassword().equals(dbEmployee.getPassword())) {
            throw new CustomException("500", "原密码输入错误");
        }
        // 4. 更新密码
        dbEmployee.setPassword(employee.getCheckpwd());
        // 5. 执行更新
        employeeMapper.updataById(dbEmployee);
    }
}